<template>
  <div class="control">
    <div class="control-top">
      <div class="control-logo">
        <!--        <el-image style="width: 100px; height: 100px" :src="url" fit="fill"/>-->
        <i class="el-icon-star-off"/>
        <span>测试标题</span>
      </div>
    </div>
    <div class="control-box">
      <el-card class="box-card" v-for="(e,index) in controls" :key="index" @click.native="choiceTeam(e.id)">
        <div slot="header">
          <div class="title">{{ e.teamName }}</div>
        </div>
        <div class="text">编号： {{ e.id }}</div>
        <div class="text">类型： {{ types[e.teamType] }}</div>
        <div class="text">创建时间： {{ e.createTime }}</div>
      </el-card>

      <el-card class="box-card" @click.native="openAddDialog=true">
        <div class="add-new">
          <i class="el-icon-circle-plus-outline"/>
          <div class="tip">添加一个新项目</div>
        </div>
      </el-card>
    </div>

    <el-dialog :visible.sync="openAddDialog" width="720px" title="创建新项目">
      <el-form :model="team" :rules="teamRules" ref="team" label-width="100px">
        <el-form-item label="活动名称" prop="teamName">
          <el-input class="al-width-500" v-model="team.teamName" maxlength="15"
                    show-word-limit placeholder="请输入项目名称"/>
        </el-form-item>
        <el-form-item label="项目类型" prop="type">
          <el-select class="al-width-500" v-model="team.teamType" placeholder="请选择类型">
            <el-option :label="v" :value="parseInt(k)" v-for="(v,k,index) in types"/>
          </el-select>
        </el-form-item>
        <el-form-item label="项目描述">
          <el-input class="al-width-500" v-model="team.description"
                    :rows="10"
                    resize="none"
                    type="textarea"
                    maxlength="100"
                    show-word-limit
                    placeholder="请输入项目描述"/>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="openAddDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitTeam">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
//https://res.wx.qq.com/mpres/zh_CN/htmledition/pages/login/loginpage/images/new_bg_logo67acac.svg
//https://wwcdn.weixin.qq.com/node/wework/images/202201062104.366e5ee28e.png
import {control_rules} from "@/api/wxqy/const";
import {addTeam, changeTeam, teamList} from "@/api/wxqy/controlApis";
import {setTeamId} from "@/utils/auth";

export default {
  name: "control",
  data() {
    return {
      openAddDialog: false,
      teamRules: control_rules,
      team: {teamType: 3},
      controls: [],
      types: {1: '微信服务号', 2: '微信订阅号', 3: '企业号'},
      config: {1: '', 2: '', 3: 'qywxhorization'},
    }
  },
  methods: {
    userTeamList() {
      teamList().then(e => {
        this.controls = e.data
      })
    },
    submitTeam() {
      this.$refs.team.validate(v => {
        if (v) {
          addTeam(this.team).then(e => {
            this.userTeamList()
            this.openAddDialog = false
          })
        }
      })
    },
    choiceTeam(teamId) {
      changeTeam(teamId)
        .then(e => {
          let need = e.data.needConfig || false, path = 'index'
          if (need) {
            let type = parseInt(e.data.type || '0')
            path = this.config[type]
          }
          setTeamId(e.data.teamId)
          this.$router.push({path: path})
        })
    }
  },
  created() {
    this.userTeamList()
  }
}
</script>

<style scoped lang="scss">
.control {

  .control-top {
    padding: 20px;
  }
}

.control-logo {
  i {
    font-size: 24px;
    margin-right: 20px;
  }
}

.control-box {
  margin: 50px 20px 20px 20px;
  display: flex;
  flex-wrap: wrap;

  .box-card {
    width: 280px;
    height: 200px;
    border-radius: 14px;
    cursor: pointer;
    margin: 10px;

    .add-new {
      height: 180px;
      justify-content: center;
      align-items: center;
      display: flex;
      flex-direction: column;

      i {
        font-size: 50px;
        color: #0fe59e;
      }

      .tip {
        font-size: 12px;
        color: #03b177;
      }
    }

    ::v-deep.el-card__header {
      padding: 10px 15px 1px;
    }

    ::v-deep.el-card__body {
      padding: 10px;
    }

    .title {
      font-size: 15px;
      font-weight: bold;
      margin: 10px;
    }

    .text {
      font-size: 13px;
      margin: 10px;
      font-weight: 500;
    }

    .item {
      padding: 18px 0;
    }
  }
}
</style>
